package com.itcast.tanhuasso.controller;

import com.itcast.tanhuasso.domain.User;
import com.itcast.tanhuasso.service.SmsService;
import com.itcast.tanhuasso.service.UserService;
import com.itcast.tanhuasso.vo.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @PachageName: com.itcast.tanhuasso.controller
 * @ClassName:
 * @Description: TODO
 * @Author: Hushi
 * @DateTime: 2021/2/721:29
 * @Version 1.0
 */
@RestController
@RequestMapping("user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("loginVerification")
    public ResponseEntity<Object> login(@RequestBody Map<String,String> parm){
        try {

            String phone = parm.get("phone");
            String yzm = parm.get("verificationCode");
            String data =  userService.login(phone,yzm);
            if (data != null){
                Map<String,Object> map = new HashMap<>();
                String[] ss = StringUtils.split(data,"|");
                map.put("token",ss[0]);
                map.put("isNew",Boolean.valueOf(ss[1]));
                return ResponseEntity.ok(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000002").errMessage("验证码校验失败").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    @GetMapping("{token}")
    public User queryUserByToken(@PathVariable("token") String token){
        return this.userService.queryUserByToken(token);
    }

}
